<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%region}}".
 *
 * @property int $id
 * @property int $parent_id 父级ID
 * @property string $name 名称
 * @property string $short_name 简称
 * @property double $longitude 经度
 * @property double $latitude 纬度
 * @property int $level 等级(1省/直辖市,2地级市,3区县,4镇/街道)
 * @property int $sort 排序
 * @property int $status 状态(0禁用/1启用)
 */
class Region extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%region}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'name', 'short_name', 'level'], 'required'],
            [['id', 'parent_id', 'level', 'sort', 'status'], 'integer'],
            [['longitude', 'latitude'], 'number'],
            [['name', 'short_name'], 'string', 'max' => 50],
            [['id'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'parent_id' => 'Parent ID',
            'name' => 'Name',
            'short_name' => 'Short Name',
            'longitude' => 'Longitude',
            'latitude' => 'Latitude',
            'level' => 'Level',
            'sort' => 'Sort',
            'status' => 'Status',
        ];
    }

    // 由id获得地址全名
    public static function getNameById($id, $name = ''){
        $res = self::findOne($id);
        if($res){
            if($name !== '')
                $name = ($res->name).','.$name;
            else
                $name = $res->name;
            $name = self::getNameById($res->parent_id, $name);
        }
        return $name;
    }
}
